package springboot;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import springboot.dao.*;
import springboot.doMain.WxDepartment;
import springboot.doMain.WxGroup;
import springboot.doMain.WxGroupUser;
import springboot.doMain.WxUser;
import springboot.http.WXWorkApi;
import springboot.service.impl.*;

import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * wxWork
 * springboot
 * Created by Blue on 2021/9/26.
 */

@RunWith(SpringRunner.class)
@SpringBootTest
public class test {
	@Autowired
	WXGroupServiceImpl wxGroup;
	
	@Autowired
	TbProblemStatusServiceImpl tbProblemStatusService;
	
	@Autowired
	WXUserServiceImpl wxUserService;
	
	@Autowired
	WxUserDao wxUserDao;
	
	@Autowired
	WxGroupDao wxGroupDao;
	
	@Autowired
	BoringmsgDao boringmsgDao;
	
	@Autowired
	WxDepartmentDao wxDepartmentDao;
	
	@Autowired
	WxGroupUserDao wxGroupUserDao;
	
	@Test
	public void shouldAnswerWithTrue() {
		String wrngRZCAAAEKjf4hI8tmcZaC7_rBv4_g = WXWorkApi.getGroupInfo("wrngRZCAAAEKjf4hI8tmcZaC7_RBv4_g");
		System.out.println("wrngRZCAAAEKjf4hI8tmcZaC7_rBv4_g = " + wrngRZCAAAEKjf4hI8tmcZaC7_rBv4_g);
		
	}
	
	
	@Test
	public void selAllExternalUser() {
		/*
		 * 查找所有外部客户
		 * */
		List<WxGroupUser> wxGroupUsers = wxGroupUserDao.selAllExternalUser();
		String token = WXWorkApi.getToken();
//		获取失败的数量
		AtomicInteger i = new AtomicInteger();
//		成功获取数据的数量
		AtomicInteger j = new AtomicInteger();
		wxGroupUsers.forEach(wxGroupUser -> {
			if (wxGroupUser.getType() == 1) {
				return;
			}
			String userId = wxGroupUser.getUserId();
			String userInfo = WXWorkApi.getUserInfo(userId, token);
			if (userInfo == null) {
				i.getAndIncrement();
				System.out.println("i = " + i);
			} else {
				
				j.getAndIncrement();
				System.out.println("j = " + j);
			}
		});
		System.out.println("i+j = " + i + "     " + j);
	}
	
	
	/*修改添加群组信息数据*/
	@Test
	public void changeWxGroup() {
		int i = changeGroupInfo(0);
		if (i != -1) {
			int i1 = changeGroupInfo(i);
			System.out.println("i1 = " + i1);
		}
	}
	
	int changeGroupInfo(int index) {
		String groupList = WXWorkApi.getGroupList();
		if (groupList == null) {
			return 0;
		}
		JSONObject jsonObject = JSON.parseObject(groupList);
		JSONArray group_chat_list = jsonObject.getJSONArray("group_chat_list");
		String token = WXWorkApi.getToken();
		if (index < 0) {
			index = 0;
		}
		for (int i = index; i < group_chat_list.size(); i++) {
			String chat_id = WXWorkApi.getGroupInfo(
							JSONObject.parseObject(group_chat_list.get(i) + "").getString("chat_id"), token);
			JSONObject groupInfo = JSON.parseObject(chat_id);
			System.out.println("i = " + i);
			int insert = wxGroup.insert(new WxGroup(groupInfo));
			
			System.out.println("insert = " + insert);
			if (insert == -1) {
				return i;
			}
		}
		return -1;
	}
	
	@Test
	public void insertWxExUser() {
		String groupList = WXWorkApi.getGroupList();
		if (groupList == null) {
			return;
		}
		JSONObject jsonObject = JSON.parseObject(groupList);
		JSONArray group_chat_list = jsonObject.getJSONArray("group_chat_list");
		String token = WXWorkApi.getToken();
		
		for (Object o : group_chat_list) {
			String chat_id = WXWorkApi.getGroupInfo(
							JSONObject.parseObject(o + "").getString("chat_id"), token);
			JSONObject groupInfo = JSON.parseObject(chat_id);
			wxGroup.insert(WxGroup.getInstance(chat_id));
			JSONArray member_list = JSONArray.parseArray(groupInfo.getString("member_list"));
			for (Object o1 : member_list) {
				JSONObject jsonObject1 = JSONObject.parseObject("" + o1);
				WxGroupUser wxGroupUser = new WxGroupUser(jsonObject1);
				wxGroupUser.setChatId(JSONObject.parseObject(o + "").getString("chat_id"));
				int insert = wxGroupUserDao.insert(wxGroupUser);
				System.out.println("insert = " + insert);
			}
		}
	}
	
	@Autowired
	WXGroupUserServiceImpl wxGroupUserService;
	
	
	@Test
	public void insertWxGroupUser() {
		/*
		 * 插入群成员
		 * */
		String groupList = WXWorkApi.getGroupList();
		if (groupList == null) {
			return;
		}
		JSONObject jsonObject = JSON.parseObject(groupList);
		JSONArray group_chat_list = jsonObject.getJSONArray("group_chat_list");
		String token = WXWorkApi.getToken();
		
		for (Object o : group_chat_list) {
			String chat_id = WXWorkApi.getGroupInfo(
							JSONObject.parseObject(o + "").getString("chat_id"), token);
			JSONObject groupInfo = JSON.parseObject(chat_id);
//			wxGroup.insert(new WxGroup(groupInfo));
			JSONArray member_list = JSONArray.parseArray(groupInfo.getString("member_list"));
			for (Object o1 : member_list) {
				JSONObject jsonObject1 = JSONObject.parseObject("" + o1);
				WxGroupUser wxGroupUser = new WxGroupUser(jsonObject1);
				wxGroupUser.setChatId(JSONObject.parseObject(o + "").getString("chat_id"));
//				System.out.println("o1 = " + o1);
//				System.out.println("wxGroupUser = " + wxGroupUser);
				int insert = wxGroupUserService.insert(wxGroupUser);
				System.out.println("insert = " + insert);
			}
		}
	}
	
	
	@Test
	public void insertUser() {
		String departmentInfo = WXWorkApi.getDepartmentInfo();
		JSONArray jsonArray = JSONArray.parseArray(departmentInfo);
		System.out.println("jsonArray = " + jsonArray);
//		jsonArray.forEach(o -> wxUserService.insert(new WxUser(JSON.parseObject(o + ""))));
		jsonArray.forEach(o -> {
			WxUser wxUser = new WxUser(JSON.parseObject(o + ""));
			System.out.println("wxUser = " + wxUser);
		});
		System.out.println("jsonArray = " + jsonArray.size());
	}
	
	@Test
	public void insertAllWxUser() {
		/*遍历所有部门获取所有用户，并插入数据库*/
		List<WxDepartment> wxDepartments = wxDepartmentDao.selAllDepartment();
		AtomicInteger i = new AtomicInteger();
		AtomicInteger j = new AtomicInteger();
		wxDepartments.forEach(wxDepartment -> {
			String departmentInfo = WXWorkApi.getDepartmentInfo(wxDepartment.getId());
			JSONArray jsonArray = JSONArray.parseArray(departmentInfo);
			jsonArray.forEach(o -> {
				int insert = wxUserService.insert(new WxUser(JSON.parseObject(o + "")));
				if (insert == 1) {
					i.getAndIncrement();
				} else if (insert == 2) {
					j.getAndIncrement();
				}
			});
		});
		/*影响行数无意义，每个员工可能存在于不同的部门当中，会有重复更新的现象
		 * 如果表中不存在则插入，存在即更新员工信息数据*/
		System.out.println(j + "影响行数 = " + i);
	}
	
	@Autowired
	WxDepartmentServiceImpl wxDepartmentService;
	
	@Test
	public void selAllDepartment() {
		/*查询所有部门分类数据*/
		List<WxDepartment> wxDepartments = wxDepartmentService.selAllDepartment();
		wxDepartments.forEach(wxDepartment -> System.out.println("wxDepartment = " + wxDepartment));
	}
	
	@Test
	public void insertDepartment() {
		/*插入部门分类数据*/
		String departmentInfo = WXWorkApi.getDepartmentList();
		JSONArray jsonArray = JSONArray.parseArray(departmentInfo);
		AtomicInteger index = new AtomicInteger();
		jsonArray.forEach(o -> {
			int insertDepartment = wxDepartmentService.insertDepartment(new WxDepartment(o + ""));
			if (insertDepartment == 1) {
				index.getAndIncrement();
			}
		});
		System.out.println("新插入部门数量 = " + index);
	}
	
	
	
}

